package rahmath.sharif.docrypt;

import rahmath.sharif.docrypt.constant.EncodingType;
import rahmath.sharif.docrypt.util.BaseCoder;
import rahmath.sharif.docrypt.util.DocryptUtil;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class TextCodeActivity extends Activity {

	@TargetApi(11)
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_text_code);
		Spinner spinner = (Spinner) findViewById(R.id.spinnerEncodingMethods);
		ArrayAdapter<EncodingType> spinnerAdapter = new ArrayAdapter<EncodingType>(
				this, android.R.layout.simple_spinner_item,
				EncodingType.values());
		spinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown);
		spinner.setAdapter(spinnerAdapter);
		if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_text_code, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.help:
			Intent helpActivity = new Intent(this, HelpActivity.class);
			startActivity(helpActivity);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void copyToClipboard(View view) {
		DocryptUtil.doCopy(this, R.id.editTextOutputText);
	}

	public void pasteFromClipboard(View view) {
		DocryptUtil.doPaste(this, R.id.editTextInputText);
	}

	public void encode(View view) {
		String encodingType = ((Spinner) findViewById(R.id.spinnerEncodingMethods))
				.getSelectedItem().toString();
		String inputText = ((EditText) findViewById(R.id.editTextInputText))
				.getText().toString();
		Docrypt docrypt = new Docrypt(true);
		docrypt.execute(encodingType, inputText);
	}

	public void decode(View view) {
		String encodingType = ((Spinner) findViewById(R.id.spinnerEncodingMethods))
				.getSelectedItem().toString();
		String inputText = ((EditText) findViewById(R.id.editTextInputText))
				.getText().toString();
		Docrypt docrypt = new Docrypt(false);
		docrypt.execute(encodingType, inputText);
	}

	private class Docrypt extends AsyncTask<String, Integer, String> {
		private boolean doEncode;

		public Docrypt(boolean doEncode) {
			this.doEncode = doEncode;
		}

		@Override
		protected String doInBackground(String... strings) {
			EncodingType encodingType = EncodingType.forName(strings[0]);
			String outputText = null;
			if (doEncode) {
				outputText = BaseCoder.encodeToString(encodingType, strings[1]);
			} else {
				outputText = BaseCoder.decodeToString(encodingType, strings[1]);
			}
			return outputText;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			((EditText) findViewById(R.id.editTextOutputText)).setText(result);
		}
	}
}
